﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QQ.InterviewQuestions.Array
{
    class SplitArray
    {
        public int[] Execute(int[] a)
        {
            if (a == null || a.Length <= 0)
            {
                return null;
            }

            int[] index = new int[a.Length];
            for (int i = 0; i < a.Length; i++)
            {
                index[i] = i;
            }

            System.Array.Sort(a, index);

            int start = 0;
            int end = 0;

            while (end < a.Length - 1)
            {
                start = end;
                while (end < a.Length && (a[end + 1] - a[end] == 0 || a[end + 1] - a[end] == 1))
                {
                    end++;
                    System.Array.Sort(index, a, start, end - start + 1);
                }
                end++;
            }


            return a;
        }
    }
}
